from PIL import Image
from PIL import ImageFilter
from pathlib import Path
import matplotlib.pyplot as plt

class ImageProcessor:#基类
    def __init__(self,image_name,parameter_list = None):
        self.image_name = image_name
        self.parameter_list = parameter_list
    def process(self,image_name):
        pass
    
class Grayscalepro(ImageProcessor):#灰化
    def process(self):
        image_filtered = self.image_name.filter(ImageFilter.BLUR)
        return image_filtered
    
class tailor(ImageProcessor):#裁剪或调整大小
    def process(self):
        if len(self.parameter_list) == 4:#box
            resize = self.image_name.crop(self.parameter_list)
            return resize
        else:#resize
            region = self.image_name.resize(self.parameter_list)
            return region
        
class dim(ImageProcessor):#模糊
    def process(self):
        return self.image_name.filter(ImageFilter.GaussianBlur)
    
class Edge_extraction(ImageProcessor):#边缘锐化
    def process(self):
        return self.image_name.filter(ImageFilter.FIND_EDGES)

class ImageShop:
    def __init__(self,image_format,image_path,parameter_list ,image_pro):
        self.image_format = image_format
        self.image_path = image_path
        self.parameter_list = parameter_list
        self.image_pro = image_pro
    def load_images(self, image_path, image_format = "jpg"):
        self.images = []
        self.image_format = image_format
        self.image_path = image_path
        image_path = Path(image_path)
        self.image_pro = []
        for file in image_path.glob("*."+image_format):#glob下载image_path下的所有image_format格式文件
            self.images.append(Image.open(file))
            #print(self.images)
    def __batch_ps(self, f):
        return f.process()
    def batch_ps(self, *args):#*arg为不确定的变量
        for im in self.images:
            for arg in args:
                if isinstance(arg, tuple): 
                    operation, parameter = arg
                    im = self.__batch_ps(tailor(im, parameter))
                else:
                    operation = arg
                    if operation == "Grayscalepro":
                        im = self.__batch_ps(Grayscalepro(im))
                    elif operation == "Edge_extraction":
                        im = self.__batch_ps(Edge_extraction(im))
                    elif operation == "dim":
                        im = self.__batch_ps(dim(im))
            self.image_pro.append(im)
            #print(self.image_pro)
    def save(self,path, image_format):
        for i in range(len(self.image_pro)):
            self.image_pro[i].save(path+"processed"+str(i+1)+'.'+image_format)
            
    def display(self, n_row, n_col, maxnum = 9,processed = True):#最多输出图片为9张
        if processed: # 展示处理前（后）的图片
            images = self.image_pro
            title = "Proccessed Images"
        else:
            images = self.images
            title = "Unproccessed Images"
        for i in range(min(len(images),maxnum)):
            plt.subplot(n_row, n_col, i+1)
            plt.imshow(images[i])
        plt.title(title)
        plt.show()
        
class TestImageShop:
    def __init__(self, image_path, image_format):
        self.image_path = image_path
        self.image_format = image_format
    def test(self, save_path,*args):
        testing = ImageShop(image_format="jpg",image_path=self.image_path,
                            parameter_list=None ,image_pro=None)
        testing.load_images(self.image_path, self.image_format) # 加载图片
        testing.display(1, 3, 9, processed = False) # 展示处理前的图片
        testing.batch_ps(*args) # 处理图片
        testing.display(1, 3, 9, processed = True) # 展示处理后的图片
        testing.save(save_path, self.image_format)
        
if __name__ == "__main__":
    test1 = TestImageShop("C:/Users/User/Desktop/code/pycode/lab6/image/", "jpg")
    test1.test("C:/Users/User/Desktop/code/pycode/lab6/image/processed/", "Edge_extraction")
    